package gui;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

import parsing.InvalidSyntaxException;

import world.Critter;
import world.CritterWorld;

public class AddCritterFrame extends JFrame {
	private CritterWorld world;
	private WorldViewPanel view;
	
	private JPanel containerPanel;
	private JTextField critterFileText;
	private JTextField addNumText;
	private Box buttonBox;
	private JButton addSelectedButton;
	private JButton addRandomButton;
	private JButton cancelButton;

	private final Color bkg = new Color(255,99,71);

	public AddCritterFrame(CritterWorld w, WorldViewPanel v) {
		world = w;
		view = v;

		setSize(500, 150);
		setTitle("Add a critter from file");
		containerPanel = new JPanel();
		containerPanel.setLayout(new GridLayout(0,1));
		containerPanel.setBorder(new LineBorder(bkg, 3));
		add(containerPanel);
		
		containerPanel.add(new JLabel("File to load new critters from:", SwingConstants.LEFT));
		critterFileText = new JTextField();
		containerPanel.add(critterFileText);
		
		buttonBox = new Box(BoxLayout.X_AXIS);
		containerPanel.add(buttonBox);
		
		addSelectedButton = new JButton("Add to selected hex");
		addSelectedButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				if (view.getSelectedLoc() != null) {
					try {
						new Critter(world, world.getHex(view.getSelectedLoc()),
								0, critterFileText.getText());
						SwingUtilities.getWindowAncestor(view).repaint();
						SwingUtilities.getWindowAncestor(addSelectedButton).dispose();
					} catch (IllegalArgumentException e1) {
						JOptionPane.showMessageDialog(SwingUtilities.getWindowAncestor(addSelectedButton),
								e1.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
					} catch (InvalidSyntaxException e1) {
						JOptionPane.showMessageDialog(SwingUtilities.getWindowAncestor(addSelectedButton),
								"The syntax of the given program is invalid.", "Error", JOptionPane.ERROR_MESSAGE);
					} catch (IOException e1) {
						JOptionPane.showMessageDialog(SwingUtilities.getWindowAncestor(addSelectedButton),
								e1.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
					}
				}
			}
		});
		
		addNumText = new JTextField("1");
		addRandomButton = new JButton("Randomly add amount");
		addRandomButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				try {
					for(int i = 0; i < Integer.parseInt(addNumText.getText()); i++)
						new Critter(world, world.getRandomEmptyHex(), 0, critterFileText.getText());
					SwingUtilities.getWindowAncestor(view).repaint();
					SwingUtilities.getWindowAncestor(addRandomButton).dispose();
				} catch (NumberFormatException e1) {
					JOptionPane.showMessageDialog(SwingUtilities.getWindowAncestor(addSelectedButton),
							"Only number characters may be entered as values.", "Error", JOptionPane.ERROR_MESSAGE);
				} catch (IllegalArgumentException e1) {
					JOptionPane.showMessageDialog(SwingUtilities.getWindowAncestor(addSelectedButton),
							e1.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
				} catch (InvalidSyntaxException e1) {
					JOptionPane.showMessageDialog(SwingUtilities.getWindowAncestor(addSelectedButton),
							"The syntax of the given program is invalid.", "Error", JOptionPane.ERROR_MESSAGE);
				} catch (IOException e1) {
					JOptionPane.showMessageDialog(SwingUtilities.getWindowAncestor(addSelectedButton),
							e1.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		
		cancelButton = new CancelButton();
		buttonBox.add(addSelectedButton);
		buttonBox.add(Box.createHorizontalStrut(20));
		buttonBox.add(addRandomButton);
		buttonBox.add(addNumText);
		buttonBox.add(Box.createHorizontalStrut(20));
		buttonBox.add(cancelButton);
		
		containerPanel.setBackground(bkg);
		setVisible(true);
	}
}
